#include "Work.h"
#include "PluginHelper.h"
#include "Context.h"
using namespace yazi::engine;

#include "Singleton.h"
using namespace yazi::utility;



Work::Work(){

}

Work::Work(const string& name, bool flag) : m_name(name), m_switch(flag) {

}

Work::~Work(){
    for(auto it = m_plugins.begin(); it != m_plugins.end(); ++it){
        destroy_func dfunc = (destroy_func)Singleton<PluginHelper>::instance()->get((*it)->get_name(), "destroy");
        dfunc(dynamic_cast<Plugin *>(*it));
    }
    m_plugins.clear();
}

void Work::append(Plugin* plugin){
    if(!plugin) return;
    m_plugins.push_back(plugin);
}

void Work::set_name(const string& name){
    m_name = name;
}

const string& Work::get_name(){
    return m_name;
}

void Work::set_switch(bool flag){
    m_switch = flag;
}

bool Work::get_switch(){
    return m_switch;
}

bool Work::run(Context& ctx){
    for(auto it = m_plugins.begin(); it != m_plugins.end(); ++it){
        if((*it)->get_switch()){
            if(!(*it)->run(ctx))
                break;
        }
    }
    return true;
}





